#ifndef ACAPELLA_SYNCHRONIZE_SEMAPHORE_WINDOWS_H
#define ACAPELLA_SYNCHRONIZE_SEMAPHORE_WINDOWS_H

#include <windows.h>

namespace acapella
{
namespace synchronize
{

class semaphore
{
	enum
	{
		MAX_COUNT = 0x0FFFFFFF
	};
public:
	semaphore(_uint32_ initCount = 0)
	{
		sem_ = CreateSemaphore(NULL, initCount, MAX_COUNT, NULL);
	}

	~semaphore()
	{
		CloseHandle(sem_);
	}

	int post()
	{
		return ReleaseSemaphore(sem_ , 1, NULL) == TRUE ? 0 : 1;
	}

	int wait()
	{
		return WaitForSingleObject(sem_, INFINITE);
	}

	int timedwait(_uint32_ timeout)
	{
		return WaitForSingleObject(sem_, timeout);
	}

private:
	HANDLE sem_;

	semaphore(const semaphore&);
	semaphore& operator=(const semaphore&);
};


}
}


#endif
